var util = require('../../utils/util.js')
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var cancelReason = '';
var orderId = '';
var frm ='';
var value1 = '突然不想买了'; 
var value2 = '重复购买了';
var value3 = '感觉还是有点贵了';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tc: 0,
    uid:'',
    token:'',
    cancels: [
      { name: '1', value: value1, checked: 'true' },
      { name: '2', value: value2, checked: 'false' },
      { name: '3', value: value3, checked: 'false' }
    ],
    orderType: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    orderId = options.orderid;
    frm = options.frm;
    var ordertype = options.ordertype ? options.ordertype : 2;

    var tc = app.get_tc();
    if(tc == 1){
      value1 = '突然不想買了';
      value2 = '重復購買了';
      value3 = '感覺還是有點貴了';
    }
    this.setData({
      ancels: [
        { name: '1', value: value1, checked: 'true' },
        { name: '2', value: value2, checked: 'false' },
        { name: '3', value: value3, checked: 'false' }
      ],
    })

    if (ordertype == 2){
      cancelReason = value2;
    };
    var titlename = tc == 1 ? '取消訂單' : '取消订单';
    wx.setNavigationBarTitle({
      title: titlename,
    });
    
    var that = this;
    //当前登录用户信息
    app.LoginUserInfo(function (res) {
      //更新数据
      that.setData({
        uid: res.uid,
        token: res.token,
        orderType: ordertype,
      });
    }, '');

  },

  cancelChange: function (e) {
    cancelReason = e.detail.value;
  },

  //取消订单
  cancelorder: function (event) {
    app.saveFormId(event.detail.formId);
    var that = this;
    var cusReason = event.detail.value.reason;
    var titlename = this.data.tc == 1 ? '請輸入取消原因' : '请输入取消原因'; 
    if(!cusReason && !cancelReason){
      wx.showToast({
        title: titlename,
      })
      return;
    }
    var reason = cancelReason;
    if(cusReason){
      reason += (';' + cusReason);
    }
    var privatekey = utilMd5.hexMD5(this.data.uid + 'cancelgoodsorder' + this.data.token);
    var data = {
      ac: "cancelgoodsorder",
      reason: reason,
      orderid: orderId,
      uid: this.data.uid,
      privatekey: privatekey
    };

    util.api({
      data: data,
      method: 'POST',
      success: function (responseData) {
        var data = responseData.data;
        var consolename = that.data.tc == 1 ? '取消訂單' : '取消订单';
        console.log(consolename, data);
        if (data.res) {
          //刷新
          var delta = 1;
          if(frm == 'detail'){
            delta = 2;
          }
          app.dialogToast('取消成功', 'success', function(){
            wx.navigateBack({
              delta: delta
            })
          })
        } else {
          app.dialog(data.msg, 'loading');
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

})